<template>
  <div :class="{'hidden':hidden}" class="pagination-container">
    <el-pagination
      :total="total"
      :background="background"
      :page-size.sync="pageSize1"
      :current-page.sync="currentPage"
      :layout="layout"
      :page-sizes="pageSizes"
      :hide-on-single-page="true"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { scrollTo } from "@/utils/scroll-to";

export default {
  name: "Pagination",
  props: {
    total: {
      required: true,
      type: Number
    },
    pageIndex: {
      type: Number,
      default: 1
    },
    pageSize: {
      type: Number,
      default: 10
    },
    background: {
      type: Boolean,
      default: true
    },
    pageSizes: {
      type: Array,
      default() {
        const result = [10, 20, 30, 50];
        if (result.indexOf(this.pageSize) === -1) {
          result.unshift(this.pageSize);
        }

        return result;
      }
    },
    layout: {
      type: String,
      default: "total, sizes, prev, pager, next, jumper"
    },
    autoScroll: {
      type: Boolean,
      default: true
    },
    hidden: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    currentPage: {
      get() {
        return this.pageIndex;
      },
      set(val) {
        this.$emit("update:pageIndex", val);
      }
    },
    pageSize1: {
      get() {
        return this.pageSize;
      },
      set(val) {
        this.$emit("update:pageSize", val);
      }
    }
  },
  methods: {
    handleSizeChange(val) {
      this.$emit("pagination", { pageIndex: this.pageIndex, pageSize: val });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    },
    handleCurrentChange(val) {
      this.$emit("pagination", { pageIndex: val, pageSize: this.pageSize });
      if (this.autoScroll) {
        scrollTo(0, 800);
      }
    }
  }
};
</script>

<style scoped>
/* .pagination-container {
  background: #fff;
  padding: 32px 16px;
} */
.pagination-container.hidden {
  display: none;
}
</style>
